import { Entity, Column, PrimaryGeneratedColumn, UpdateDateColumn, ManyToOne, JoinColumn, OneToMany } from 'typeorm';
import { ApiProperty} from '@nestjs/swagger';
import {
  IsOptional,
  IsString,
  MaxLength,
  MinLength,
  IsNotEmpty,
  IsEmail,
  IsPhoneNumber,
  IsMobilePhone,
  IsDate,
  ValidateNested, IsDateString
} from "class-validator";
import { Coupon } from './coupon.entity';
import { User } from './user.entities';
import { Order } from './order.entities';

@Entity()
export class UserCoupon {
  @PrimaryGeneratedColumn({type: 'mediumint'})
  id: number;

  @IsOptional()
  @ApiProperty()
  @Column({type: 'tinyint', unsigned: true})
  coupon_id:number;

  @IsOptional()
  @ApiProperty()
  @Column({type: 'varchar' ,length: 20})
  coupon_number:number;

  @IsOptional()
  @ApiProperty()
  @Column({type: 'int'})
  user_id: number;

  @IsOptional()
  @ApiProperty()
  @Column({type: 'datetime'})
  used_time: number;

  @IsOptional()
  @ApiProperty()
  @Column({type: 'datetime'})
  add_time: number;

  @IsOptional()
  @ApiProperty()
  @Column({type: 'mediumint', unsigned: true})
  order_id: number;

  @IsOptional()
  @ApiProperty()
  @Column({type: 'varchar', length: 64})
  source_key: string;

  @IsOptional()
  @ApiProperty()
  @Column({type: 'int', comment: "发券人"})
  referrer: number;

  @IsOptional()
  @ApiProperty()
  @Column({type: 'tinyint', unsigned: true, comment: "状态 1. 可用 2. 已用 3. 过期"})
  coupon_status: number;

  @ManyToOne(type => Coupon, coupon => coupon.userCoupon)
  @JoinColumn({
    name:"coupon_id"
  })
  coupon: Coupon;

  @ManyToOne(type => User, user => user.userCoupon)
  @JoinColumn({
    name:"user_id"
  })
  user: User;

  @ManyToOne(type => Order, order => order.userCoupon)
  @JoinColumn({
    name:"order_id"
  })
  order: Order;
}
